package tech.aistar.prj.controller;

import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.service.IVideoService;
import tech.aistar.prj.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(urlPatterns = "/video/personalView")
public class PersonalViewController extends HttpServlet {
    private IVideoService videoService = new VideoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Video> videoList = (List<Video>) req.getAttribute("videos");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");
        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<title>bootstrap是一款响应式的UI[User-Interface]框架");out.println("</title>");
        out.println("<!-- 引入bootstrap.css - 开发环境/bootstrap.min.css - 生产环境-->");
        out.println("<link rel='stylesheet' href='/st/plugins/bootstrap-3.4.1-dist/css/bootstrap.css'>");

        out.println("<link rel='stylesheet' href='/st/css/video/personal.css'>"); out.println("</head>");
        out.println("<body>");
        out.println("<div class='container-fluid'>");

        //放标签页 - 头部
        out.println("   <ul id='myTab' class='nav nav-tabs'>");
        out.println("       <li class='active'>");
        out.println("       <a href='#home' data-toggle='tab'>已发布");out.println("</a>");
        out.println("       </li>");
        out.println("       <li>");out.println("<a href='#ios' data-toggle='tab'>未发布");out.println("</a>");out.println("</li>");
        out.println("   </ul>");


        //最好的做法 - 和db交互俩次
        //where status = 0   => list1 -> request
        //where status = 2   => list2 -> request

        out.println("   <div id=\"myTabContent\" class=\"tab-content\">");
        out.println("       <div class=\"tab-pane fade in active\" id=\"home\">");
        out.println("           <div class='row' id='personal'>");
        //已发布
        videoList.forEach(e-> {
            if(e.getStatus()==0){
                out.println("   <div class='col-md-3'>");
                out.println("       <div class='thumbnail'>");
                //TODO... 隐藏域...
                out.println("       <input type='hidden' value='"+e.getId()+"'>");
                out.println("       <img src='/st/imgs/"+e.getVideoUrl()+"' alt='通用的占位符缩略图'>");
                out.println("<div class='caption'>");
                out.println("           <h3 class='text-center text-info'>"+e.getTitle()+"</h3>");
                out.println("           <p class='text-center text-success'>点赞量"+e.getLikes()+"</p>");
                out.println("           <p class='text-center text-success'>评分:<span class='text-danger'>"+e.getScore()+"</span></p>");
                out.println("           <p class='text-center'>");
                out.println("               <a href='/st/video/del?id="+e.getId()+"' class='text-danger glyphicon glyphicon-trash'></a>");
                out.println("           </p>");

                out.println("       </div>");
                out.println("   </div>");
                out.println("</div>");
                //out.println("</div>");
            }
        });
        out.println("</div>");
        out.println("</div>");

        out.println("<div class=\"tab-pane fade\" id=\"ios\">");
        out.println("<div class='row' id='personal'>");

        for (Video e : videoList) {
            if(e.getStatus()==2){
                out.println("   <div class='col-md-3'>");
                out.println("       <div class='thumbnail'>");
                //TODO...
                out.println("       <input type='hidden' value='"+e.getId()+"'>");
                out.println("       <img src='/st/imgs/"+e.getVideoUrl()+"' alt='通用的占位符缩略图'>");
                out.println("<div class='caption'>");
                out.println("           <h3 class='text-center text-info'>"+e.getTitle()+"</h3>");
                out.println("           <p class='text-center text-success'>点赞量"+e.getLikes()+"</p>");
                out.println("           <p class='text-center text-success'>评分:<span class='text-danger'>"+e.getScore()+"</span></p>");
                out.println("           <p class='text-center'>");
                out.println("               <a class='text-danger' href='/st/video/change?id="+e.getId()+"'>发布</a>");
                out.println("           </p>");
                out.println("       </div>");
                out.println("   </div>");
                out.println("</div>");
                //out.println("</div>");
            }
        }

        out.println("</div>");
        //begin...

        out.println("</div>");
        //row结束...
        out.println("</div>");
        //end
        out.println("</div>");


        out.println("</div>");

        out.println("<script src='/st/plugins/jquery.min.js'>");out.println("</script>");

        out.println("<!-- 引入bootstrap.js/bootstrap.min.js,bootstrap依赖于jquery框架 -->");
        out.println("<script src='/st/plugins/bootstrap-3.4.1-dist/js/bootstrap.js'>");out.println("</script>");
        out.println("<script src='/st/js/personal/personal.js'>");out.println("</script>");
        out.println("</body>");
        out.println("</html>");
    }


}
